package com.juzhencms.apps.busi.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.juzhencms.apps.base.common.cache.CacheSource;
import com.juzhencms.apps.base.common.database.DBTable;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class DictionaryService extends CommonService{

	
	
	//通过字典标识获取字典值List
	@Cacheable(value=CacheSource.DB_CACHE,key="#root.targetClass+'.'+#root.methodName+':'+#identifier")
	public List<HashMap> getDictionaryItemList(String identifier){
		if(identifier == null || identifier.equals("")){
			return null;
		}
		String querySql = "select id,title,name from " + DBTable.dictionary +" where del_flag = '0' and name = ?";
		Map dictionary= this.getJdbcDao().queryRow(querySql,new Object[] {identifier});
		List dictionaryItemList=null;
		if(dictionary != null && !dictionary.isEmpty()){
			dictionaryItemList=this.getJdbcDao().querySql("select value as id,text from "+DBTable.dictionaryItem+" where del_flag = '0' and dictionary_id = ? ", new Object[]{dictionary.get("id").toString()});
		}
		return dictionaryItemList;
	}
	@Cacheable(value=CacheSource.DB_CACHE,key="#root.targetClass+'.'+#root.methodName+':'+#dictionaryName+':'+#value")
	public Map getDictionaryItem(String dictionaryName, String value){
		String sql="select i.value as id,i.text as text,i.description from "+DBTable.dictionaryItem+" as i join "+DBTable.dictionary+" as d on d.name=? and i.value=? and i.dictionary_id=d.id where i.del_flag=0 limit 0,1";
		Map record=this.getJdbcDao().queryRow(sql,new Object[] {dictionaryName,value});
		return record;
	}

	
	public String getText(List itemList,String value) {	
		Map itemvar=this.getItem(itemList,value);
		if(itemvar!=null) {
			return itemvar.get("text").toString();
		}else {
			return "";
		}
		
	}
	
	public String getCheckBoxText(List<HashMap> itemList,String values) {
		if(values==null) {
			return "";
		}
		
		
		String text="";

		String[] tmpArr=values.split(",");
		for(int i=0;i<tmpArr.length;i++) {
			String tmpText=getText(itemList, tmpArr[i]);
			if(tmpText!=null) {
				if(text.equals("")) {
					text=tmpText;
				}else {
					text+=","+tmpText;
				}
			}
		}
		
		return text;
		
	}
	
	public Map getItem(List param, String value) {
		if((List) param !=null) {
			for(int i=0;i<param.size();i++){
				Map itemvar=(Map) param.get(i);
				String itemid=itemvar.get("id").toString();
				if( itemid.equals(value)){
					return itemvar;
				}
			}
		}	
		return null;
	}
	
	public void setDictionaryText(Map record,String fieldName,List dictionaryList) {
		if(record!=null) {
			if(record.get(fieldName)!=null) {
				String value=record.get(fieldName).toString();
				record.put(fieldName+"_text", this.getText(dictionaryList, value));	
				return;
			}
			record.put(fieldName+"_text", "");
		}
	}
	
	public void setDictionaryText(List dataList,String fieldName,List dictionaryList) {
		if(dataList!=null) {
			for(int i=0;i<dataList.size();i++) {
				Map record=(Map)dataList.get(i);
				this.setDictionaryText(record, fieldName,dictionaryList);
			}
		}
	}
	

}
